package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import modele.PreinscriptionSession;

public class PreinscriptionSessionDAO {

	private static Connection connection;
	private static final String ID_SESSION = "id_session";
	private static final String ID_PERSONNE = "id_personne";
	private static final String PREREQUIS = "prerequis";
	private static final String DATE_INSCRIPTION = "date_inscription";
	private static final String ETAT = "etat";
	static final String SQL_INSERER = "INSERT INTO preinscription (id_session,id_personne,prerequis,date_inscription,etat) VALUES (?, ?, ?, ?, ?);";
	static final String SQL_MODIFIER = "UPDATE preinscription SET  `libelle` = ? WHERE  `preinscription`.`id_preinscription` = ?";
	static final String SQL_SUPPRIMER_PAR_ID = "DELETE FROM preinscription WHERE `preinscription`.`id_preinscription` = ?";
	static final String SQL_CHERCHER_PAR_ID = "SELECT * FROM preinscription WHERE id_Session= ? and id_Personne=?";
	static final String SQL_LISTER_PREINSCRIPTION = "SELECT * FROM preinscriptionORDER BY `id_preinscription` ASC";

	public static void inserer(PreinscriptionSession s) throws SQLException {
		Connection connection = FabriqueDAO.getConnection();
		PreparedStatement pt = connection.prepareStatement(SQL_INSERER);

		pt.setInt(1, s.getId_session());
		pt.setInt(2, s.getId_personne());
		pt.setString(3, s.getPrerequis());
		pt.setDate(4, s.getDate_inscription());
		pt.setInt(5, s.getEtat());

		pt.executeUpdate();
		pt.close();
	}

	public static PreinscriptionSession lireParId(int idSession,int idPersonne) throws SQLException {
		Connection connection = FabriqueDAO.getConnection();

		PreinscriptionSession result = null;

		PreparedStatement pt = connection.prepareStatement(SQL_CHERCHER_PAR_ID);
		pt.setInt(1, idSession);
		pt.setInt(2, idPersonne);
		ResultSet rs = pt.executeQuery();
		if (rs.next()) {

			result = setInscriptionaSessionValues(rs);

		}

		rs.close();
		pt.close();

		return result;
	}

	private static PreinscriptionSession setInscriptionaSessionValues(ResultSet rs)
			throws SQLException {
		PreinscriptionSession is = new PreinscriptionSession();
		is.setId_session(rs.getInt(ID_SESSION));
		is.setId_personne(rs.getInt(ID_PERSONNE));
		is.setPrerequis(rs.getString(PREREQUIS));
		is.setDate_inscription(rs.getDate(DATE_INSCRIPTION));
		is.setEtat(rs.getInt(ETAT));

		return is;
	}
}

// RECHERCHE PAR PERSONNE + AJOUTE DANS LA FENETRE
/* public Preinscription chercheParPERSONNE(String unNom){ */
/*
 * public static Personne lireParId(int i){
 * 
 * Personne p = null; try { connection = FabriqueDAO.getConnection();
 * 
 * 
 * String s = "SELECT * FROM `personne` WHERE nom = '"+i+"'"; //Requete SQL
 * 
 * Statement ordre = connection.createStatement();
 * 
 * ResultSet res = ordre.executeQuery(s); //Query pour les select puisqu'il rend
 * un tableau
 * 
 * res.next(); p = new Personne(); p.setNom(res.getString("nom"));
 * p.setPrenom(res.getString("prenom")); //p.setAge(res.getInt("age"));
 * p.setTelephone(res.getString("telephone"));
 * p.setAdresse(res.getString("adresse"));
 * p.setCode_postal(res.getString("code_postal"));
 * p.setVille(res.getString("ville")); p.setEmail(res.getString("email"));
 * p.setPassword(res.getString("password"));
 * 
 * } catch (SQLException e2){
 * System.out.println("La commande n'a pas fonctionn�.");
 * 
 * } return p;
 * 
 * }
 * 
 * // AJOUT A LA BDD public static void ajouter(Preinscription p)throws
 * SQLException {
 * 
 * try{ connection =FabriqueDAO.getConnection(); Statement ordre
 * =connection.createStatement(); //requete SQL String s =
 * "UPDATE 'preinscription' SET preinscription.id_personne=personne.id_personne"
 * ; System.out.println(s); ordre.executeUpdate(s); } }
 * 
 * 
 * 
 * 
 * //AJOUT A SESSION public static void inserer(Preinscription p) throws
 * SQLException{
 * 
 * try{ connection =FabriqueDAO.getConnection(); Statement ordre
 * =connection.createStatement(); //requete SQL String s =
 * "UPDATE 'preinscription' SET preinscription.id_personne=personne.id_personne"
 * ; System.out.println(s); ordre.executeUpdate(s);
 * 
 * 
 * }
 * 
 * } }
 * 
 * /* //supprimer une personne selon son id public void supprimer(int id) throws
 * SQLException {
 * 
 * PreparedStatement p = getPreparedStatement(SQL_SUPPRIMER); p.setInt(1, id);
 * p.executeUpdate();
 * 
 * // close p.close();
 * 
 * }
 * 
 * public ArrayList<Preinscription> listerPersonne() throws SQLException; //
 * List
 * 
 * } private static Preinscription setFormationValues(ResultSet rs) throws
 * SQLException { Preinscription p= new Preinscription();
 * p.setId_preinscription(rs.getInt(ID_PREINSCRIPTION));
 * p.setId_personne(rs.getInt(ID_PERSONNE));
 * p.setPreinscription(rs.getString(PREINSCRIPTION));
 * p.setDate_Inscription(rs.getString(DATE_INSCRIPTION));
 * p.setEtat(rs.getString(ETAT));
 * p.setCertification(rs.getString(CERTIFICATION));
 * p.setPrequis(rs.getString(PREREQUIS)); return p; } }
 */